from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, Field, HttpUrl

from typing import List
from pydantic import BaseModel


class ImageProcessItem(BaseModel):
    img: str = Field(
        "https://ty-huanlian.oss-cn-shanghai.aliyuncs.com/face_replace/20230720/5cfd3b3887c77c89f65eaf64f73af1ab.png",
        description="用户的头像")
    obj_img: str = Field(
        "https://ty-huanlian.oss-cn-shanghai.aliyuncs.com/face_replace/20230628/wecom-temp-d849d6f5ba80f73c294b01ca4ea86bce.png",
        description="目标换脸的图像")
    shuiyin: bool = Field(True, description="是否需要加水印")
    oss_name: str = Field("ali", description="OSS服务的名称,只支持ali和qiniu")


class ImageProcessResult(BaseModel):
    res_url: str
    all_url: str
    duration: float
    # 错误信息的字段,默认是空
    error_message: Optional[str] = None


class RoopResult(BaseModel):
    task_id: str = Field(..., description="任务的ID")


class RoopItem(BaseModel):
    video_url: str = Field(
        "http://usfile.chaotuapp.com/upload/sync/594c2bf8a9af19c9c7f062ba78a4fa11.mp4",
        description="视频的URL地址")
    user_img_url: str = Field(
        "https://ty-huanlian.oss-cn-shanghai.aliyuncs.com/face_replace/20230720/5cfd3b3887c77c89f65eaf64f73af1ab.png",
        description="用户图片的URL地址")
    frames: int = Field(15, description="帧数")
    callback: bool = Field(True, description="是否有回调通知")
    shuiyin: bool = Field(True, description="是否需要加水印")
    oss_name: str = Field("ali", description="OSS服务的名称,只支持ali和qiniu")


class FaceInfo(BaseModel):
    index_id: int
    bbox: List[float]
    # gender: int
    # age: int

    class Config:
        json_schema_extra = {
            "example": {
                "index_id": 0,
                "bbox": [378.60913, 286.49207, 675.0079, 670.69604],
                # "gender": 1,
                # "age": 22
            }
        }


class FaceDetectItem(BaseModel):
    img_url: str = Field(
        "https://ty-huanlian.oss-cn-shanghai.aliyuncs.com/test/WX20230831-151640%402x.png",
        description="需要分析的人脸图片地址")


class VideoUrlItem(BaseModel):
    video_url: str = Field(
        "https://ty-aihuanlian.oss-cn-shanghai.aliyuncs.com/test/RPReplay_Final1699878287.mov",
        description="需要解析的视频的地址")
    oss_name: str = Field("ali", description="OSS服务的名称,只支持ali和qiniu")


class FaceInfoResponseModel(BaseModel):
    status: str
    message: str
    faces: List[FaceInfo]

    class Config:
        json_schema_extra = {
            "example": {
                "status": "success",
                "message": "Faces detected successfully.",
                "faces": [
                    {
                        "index_id": 0,
                        "bbox": [378.60913, 286.49207, 675.0079, 670.69604],
                        # "gender": 1,
                        # "age": 22
                    },
                    # ... 其他的脸部数据
                ]
            }
        }


        
# 定义face数据的模型
class ManyFaceData(BaseModel):
    index: int = Field(..., title="人脸索引位置", description="从0开始,默认从左往右数")
    face_image_url: str = Field(..., title="用户人脸图")

    class Config:
        json_schema_extra = {
            "example": {
                "index": 0,
                "face_image_url": "http://usfile.chaotuapp.com/uploads/android/user/1693484879724.jpg"
            }
        }


# 定义请求体的模型
class ManyFaceRequestData(BaseModel):
    source_image: str = Field(..., title="源图地址", description="希望换脸的图,可以多张人脸,默认从左往右")
    faces_data: List[ManyFaceData]
    # 加一个水印的字段,bool类型,默认为True
    shuiyin: bool = Field(True, description="是否需要加水印")
    oss_name: str = Field("ali", description="OSS服务的名称,只支持ali和qiniu")

    class Config:
        json_schema_extra = {
            "example": {
                "source_image": "https://ty-huanlian.oss-cn-shanghai.aliyuncs.com/test/WX20230831-151640%402x.png",
                "faces_data": [
                    {
                        "index": 0,
                        "face_image_url": "http://usfile.chaotuapp.com/uploads/android/user/1693484879724.jpg"
                    }
                    ,
                    {
                        "index": 1,
                        "face_image_url": "http://usfile.chaotuapp.com/uploads/android/user/1693484879724.jpg"
                    },
                ],
                "shuiyin": True,
                "oss_name": "ali"
            }
        }
